from django.http import HttpResponse
from django.shortcuts import render
from requests import Response
from rest_framework.generics import GenericAPIView, ListAPIView, ListCreateAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin
from rest_framework.viewsets import ModelViewSet
from rest_framework import viewsets

from .pagination import MyPageNumberPagination
from .serializer import WipoInfoSerializer
from .serializer import WipoImgsSerializer
from .serializer import WipoSettingSerializer
from .serializer import WipoSpiderSerializer

# Models
from .models import WipoInfo
from .models import WipoImgs
from .models import WipoSetting
from .models import WipoSpider

from django.db.models import F

from django.db import connection


# Create your views here.
def index(request):
    return render(request, "designdb_app/index.html")


# API_View 开始


class WipoInfo_ApiView(ModelViewSet, CreateModelMixin, ListModelMixin):
    # 使用SQL调用数据
    queryset = WipoInfo.objects.raw(
        "SELECT wipo_Info.*, GROUP_CONCAT(wipo_imgs.ImgDownLoadUrl) as Imgs FROM  wipo_Info LEFT JOIN wipo_imgs on wipo_imgs.info_id=wipo_Info.Id GROUP BY wipo_Info.Id"
    )  # 指定查询结果集
    serializer_class = WipoInfoSerializer  # 指定序列化器
    pagination_class = MyPageNumberPagination


class WipoImgs_ApiView(ModelViewSet):
    queryset = WipoImgs.objects.all()  # 指定查询结果集
    serializer_class = WipoImgsSerializer  # 指定序列化器


class WipoSetting_ApiView(ModelViewSet):
    queryset = WipoSetting.objects.all()  # 指定查询结果集
    serializer_class = WipoSettingSerializer  # 指定序列化器


class WipoSpider_ApiView(ModelViewSet):
    queryset = WipoSpider.objects.all()  # 指定查询结果集
    serializer_class = WipoSpiderSerializer  # 指定序列化器

# API_View 结束
